<?php
class QuestionsController extends AppController {
	var $name = 'Questions';
	
	/**
	 * Display all non deleted questions for the given survey
	 * 
	 * @param object $survey_id [optional]
	 * @return 
	 */
	function index($survey_id = null){
		// Make sure the currently authorized user owns the given survey
		if(! $this->_doesSurveyBelongToAuthedUser($survey_id)){
			$this->redirect(array('controller' => 'surveys', 'action' => 'index'));
			return;
		}
		
		// Grab only the questions associated with this survey and store them in the 'questions' variable
		$this->Question->bindModel(array('hasOne' => array('QuestionsSurvey')));
		$questionData =	$this->Question->find('all', array(
				'fields' => array('Question.*'),
				'conditions'=>array('QuestionsSurvey.survey_id'=>$survey_id, // id of survey
									'Question.isDeleted' => '0'), // make sure the question isn't deleted
				'order'=>array('Question.viewOrder ASC')
		));
		
		$this->set('questions', $questionData);
		$this->set('survey_id', $survey_id);
	}
	
	/**
	 * Delete a question with the given id
	 * 
	 * @param object $question_id [optional]
	 * @param object $survey_id [optional]
	 * @return 
	 */
	function delete($question_id = null, $survey_id = null) {
		// Make sure the currently authorized user actually owns the survey
		if(! $this->_doesSurveyBelongToAuthedUser($survey_id)){
			$this->redirect(array('controller' => 'surveys', 'action' => 'index'));
			return;
		}
		
		//Set isDeleted = 1 for this question
		$this->Question->id = $question_id;
		$this->data['Question']['isDeleted'] = 1;
		
		$this->Question->save($this->data);
		
		$this->redirect(array('controller' => 'surveys', 'action' => 'edit', 'id' => $survey_id));
	}
	
	/**
	 * Move a question up in order
	 * 
	 * @param object $question_id [optional]
	 * @param object $survey_id [optional]
	 * @param object $view_order [optional]
	 * @return 
	 */
	function moveUp($question_id = null, $survey_id = null, $view_order = null) {
		// Make sure the currently authorized user actually owns the survey
		if(! $this->_doesSurveyBelongToAuthedUser($survey_id)){
			$this->redirect(array('controller' => 'surveys', 'action' => 'index'));
			return;
		}
		
		$this->Question->bindModel(array('hasOne' => array('QuestionsSurvey')));
		
		//Get the viewOrder to swap with
		$data = $this->Question->find('first', array(
			'fields' => array('Question.viewOrder'),
			'order' => array('Question.viewOrder DESC'),
			'conditions' => array('Question.viewOrder <' => $view_order,
								  'QuestionsSurvey.survey_id' => $survey_id,
								  'Question.isDeleted' => '0')
			)
		);
		
		if($data)
		{
			$newViewOrder = $data['Question']['viewOrder'];
			$oldId = $data['Question']['id'];
				
			//Set this question to have the new vieworder
			$this->Question->id = $question_id;
			$this->data['Question']['viewOrder'] = $newViewOrder;
			$this->Question->save($this->data);
			
			//Set the other question to have the current viewOrder
			$this->Question->id = $oldId;
			$this->data['Question']['viewOrder'] = $view_order;
			$this->Question->save($this->data);
		}
		
		$this->redirect(array('controller' => 'surveys', 'action' => 'edit', 'id' => $survey_id));
	}
	
	/**
	 * Move a question down in order
	 * 
	 * @param object $question_id [optional]
	 * @param object $survey_id [optional]
	 * @param object $view_order [optional]
	 * @return 
	 */
	function moveDown($question_id = null, $survey_id = null, $view_order = null) {
		// Make sure the currently authorized user actually owns the survey
		if(! $this->_doesSurveyBelongToAuthedUser($survey_id)){
			$this->redirect(array('controller' => 'surveys', 'action' => 'index'));
			return;
		}
		
		$this->Question->bindModel(array('hasOne' => array('QuestionsSurvey')));
		
		//Get the viewOrder to swap with
		$data = $this->Question->find('first', array(
			'fields' => array('Question.viewOrder'),
			'order' => array('Question.viewOrder ASC'),
			'conditions' => array('Question.viewOrder >' => $view_order,
								  'QuestionsSurvey.survey_id' => $survey_id,
								  'Question.isDeleted' => '0')
			)
		);
		
		if($data)
		{
			$newViewOrder = $data['Question']['viewOrder'];
			$oldId = $data['Question']['id'];
				
			//Set this question to have the new vieworder
			$this->Question->id = $question_id;
			$this->data['Question']['viewOrder'] = $newViewOrder;
			$this->Question->save($this->data);
			
			//Set the other question to have the current viewOrder
			$this->Question->id = $oldId;
			$this->data['Question']['viewOrder'] = $view_order;
			$this->Question->save($this->data);
		}
		
		$this->redirect(array('controller' => 'surveys', 'action' => 'edit', 'id' => $survey_id));
	}
	
	/**
	 * Checks to see if the specified survey belongs to the logged in user
	 * 
	 * @param object $survey_id
	 * @return 
	 */
	function _doesSurveyBelongToAuthedUser($survey_id){
		$this->loadModel('Survey');
		
		$active_user = $this->Auth->user();
		$surveys = $this->Survey->find('all', array(
			'conditions' => array('Survey.id' => $survey_id, 'Survey.created_by' => $active_user['User']['id']),
		));
		
		if(empty($surveys)){
			$this->Session->setFlash('Could not perform the specified operation. Please try again');
			return false;
		}
		else return true;
	}
	
}
?>